<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="课程名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="课程类型" prop="tag">
            <el-select v-model="ruleForm.tag" placeholder="请选择课程类型">
                <el-option label="计算机科学与技术" value="计算机科学与技术"></el-option>
                <el-option label="艺术设计" value="艺术设计"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="开课学期" prop="semester">
            <el-select v-model="ruleForm.semester" placeholder="请选择开课学期">
                <el-option label="2020 年秋季学期" value="2020 年秋季学期"></el-option>
                <el-option label="2020 年夏季学期" value="2020 年夏季学期"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="课程描述" prop="description">
            <el-input type="textarea" v-model="ruleForm.description" rows="6"></el-input>
        </el-form-item>
        <el-form-item label="课程封面">
            <el-upload
                class="upload-demo"
                limit=1
                drag
                action="https://jsonplaceholder.typicode.com/posts/"
                multiple>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交修改</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        
    </el-form>
</template>

<script>

import courseApi from '@/api/course'

export default {
    name: 'CourseManage',
    data() {
    return {
        ruleForm: {
            name: '',
            tag: '',
            semester: '',
            resource: '',
            description: ''
        },
        rules: {
            name: [
                { required: true, message: '请输入课程名称', trigger: 'blur' },
                { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
            ],
            semester: [
                { required: true, message: '请选择开课学期', trigger: 'change' }
            ],
            tag: [
                { required: true, message: '请选择课程类型', trigger: 'change' }
            ],
            description: [
                { required: true, message: '请填写课程描述', trigger: 'blur' }
            ]
        }
    };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                console.log('ruleForm', this.ruleForm)
                courseApi.editCourse(this.ruleForm).then(response => {
                    console.log('editCourse', response.data)
                })
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>
  